<template>
	<view class="app_Cont pointsContainer">
		<view class="showCont" v-if="showCont">
			<view class="headCont">
				<view class="erweima" @click="showShareImg = true">
					<view class="iconfont icon-xiaochengxuma"></view>
					<view>推广海报</view>
				</view>
				<view class="userCont">
					<view class="userHeadImg">
						<image :src="formData.head_img" ></image>
					</view>
					<view class="userHeadName">
						<view>{{formData.nick_name}}<text v-if="formData.is_consignee==2">（{{addressInfo.consignee_name}}）</text></view>
						<view class="iconfont icon-bianji" @click="showEditFn"></view>
					</view>
				</view>
				<view class="shareCont">
					<view class="cardCont">
						<view class="cardItem" @click="shareRecordFn">
							<view class="cardNum">{{formData.total_integral}}</view>
							<view class="cardName">总积分</view>
						</view>
						<view class="cardItem" @click="changeRecordFn">
							<view class="cardNum">{{formData.y_integral}}</view>
							<view class="cardName">已兑换</view>
						</view>
						<view class="cardItem">
							<view class="cardNum">{{formData.s_integral}}</view>
							<view class="cardName">剩余积分</view>
						</view>
					</view>
				</view>
			</view>
			<view class="pointsCont">
				<view class="pointsHead">
					<view class="pointsNotice">
						<u-notice-bar direction="column" fontSize="24rpx" color="#3f55cc" bgColor="rgba(0,0,0,0)" :text="noticeArr"></u-notice-bar>
					</view>
					<view class="pointsHeadName" @click="showTip = true">
						<text>积分商城</text>
						<text class="iconfont icon-yiwenshuoming"></text>
					</view>
				</view>
				<view class="pointsList">
					<view class="pointsItem" :key="index" v-for="(item,index) in listArr">
						<view class="itemImg">
							<view class="is_dq" v-if="item.goods_status==3">
								<view>活动结束</view>
							</view>
							<image mode="widthFix" @click="showImgFn(item)" :src="item.goods_img.split(',')[0]"></image>
						</view>
						<view class="itemName">{{item.goods_name}}</view>
						<view class="itemDes">{{item.dh_count}} 人兑换</view>
						<view class="itemCtrl">
							<view class="itemCtrlNum">
								<text class="iconfont icon-zcpt-jifenguanli"></text>
								<text>{{item.integral_sum}}</text>
							</view>
							<view :class="item.goods_status==1?'subPoints':'subPoints disBtn'" @click="pointsSub(item)">兑换</view>
						</view>
					</view>
				</view>
				<noData v-if="listArr.length==0"></noData>
			</view>
		</view>
		<subLoad v-if="showSub"></subLoad>
		<load v-if="!showCont"></load>
		<u-popup :show="showTip" @close="showTip = false" :closeOnClickOverlay="true" mode="center" round="10">
			<view class="tipMask" style="width: 600rpx">
				<view class="tipName">积分规则说明</view>
				<view class="tipNum">{{formData.tgsm.tgcgjf}}积分/人</view>
				<view class="tipDes">{{formData.tgsm.tgsm}}</view>
			</view>
		</u-popup>
		<u-popup :show="showShareImg" @close="showShareImg = false" :closeOnClickOverlay="true" mode="center" round="10">
			<view class="tipMask" style="width: 300px">
				<image mode="widthFix" :src="formData.poster"></image>
				<view class="saveBtn" @click="downFn">保存推广海报</view>
			</view>
		</u-popup>
		<u-popup :show="showSubMask" @close="showSubMask = false" :closeOnClickOverlay="true" :safeAreaInsetBottom="false" mode="bottom" :round="10">
			<view class="pointsMask formCont">
				<view class="poinsImg">
					<image mode="widthFix" v-if="activeSub.goods_img" :src="activeSub.goods_img.split(',')[0]"></image>
				</view>
				<view class="poinsName">{{activeSub.goods_name}}</view>
				<view class="poinsNum">
					<text>所需积分：</text>
					<text class="iconfont icon-zcpt-jifenguanli"></text>
					<text>{{activeSub.integral_sum}}</text>
				</view>
				<view class="maskTitle">
					<view class="weitText">收货信息</view>
					<view class="blueText" @click="pointsEditAdd">编辑</view>
				</view>
				<view class="maskAddress">
					<view class="addName">收货人：</view>
					<view class="addDes">{{addressInfo.consignee_name}}</view>
				</view>
				<view class="maskAddress">
					<view class="addName">手机号：</view>
					<view class="addDes">{{addressInfo.consignee_mobile}}</view>
				</view>
				<view class="maskAddress">
					<view class="addName">收货地址：</view>
					<view class="addDes">{{addressInfo.consignee_address}}</view>
				</view>
				<view class="sureBtn" @click="surePointsFn">确认兑换</view>
			</view>
		</u-popup>
		<u-popup :show="showEdit" @close="showEdit = false" :closeOnClickOverlay="true" :safeAreaInsetBottom="false" mode="bottom" :round="10">
			<view class="formCont">
				<view class="formHead">收货信息</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">收货人</view>
				    </view>
					<view class="inputValue">
						<input maxlength="20"  v-model="addressInfo.consignee_name" placeholder="请输入收货人姓名"  />
					</view>
				</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">手机号</view>
				    </view>
					<view class="inputValue">
						<input maxlength="11"  type="number" v-model="addressInfo.consignee_mobile" placeholder="请输入手机号"  />
					</view>
					<view v-if="checkPhoneFn(addressInfo.consignee_mobile)" class="iconfont icon-gouxuan1"></view>
				</view>
				<view class="formItem">
					<view class="inputName">
					    <view class="mustIcon">*</view>
					    <view class="inputNameText">收货地址</view>
				    </view>
					<view class="inputValue">
						<input maxlength="50" v-model="addressInfo.consignee_address" placeholder="请输入收货地址"  />
					</view>
				</view>
				<view class="sureBtn" @click="saveFn">保存信息</view>
			</view>
		</u-popup>
		<u-overlay :show="showImg" opacity="0.8" @click="showImg = false">
			<view class="war">
				<view class="bannerCont">
					<u-swiper
						:list="bannerList"
						indicator
						height="700rpx"
						indicatorMode="line"
						circular
					></u-swiper>
				</view>
			</view>
		</u-overlay>
		<u-notify ref="uNotify"></u-notify>
		<u-modal :show="showModal" :showCancelButton="true" @cancel="showModal = false" title="操作提示" @confirm="modalSure"  :content='modalCont'></u-modal>
	</view>
</template>

<script>
	import subLoad from '@/components/sub.vue'
	import load from '@/components/load.vue'
	import noData from '@/components/noData.vue'
	import api from '@/http/api.js'
	import ruleCheck from '@/utils/ruleCheck.js'
	export default {
		components:{subLoad,load,noData},
		data() {
			return {
				showCont:false,
				showImg:false,
				bannerList:[],
				showModal:false,
				showTip:false,
				showEdit:false,
				editType:1,//1为正常编辑地址，2为点击兑换编辑地址
				showShareImg:false,
				showSub:false,
				modalCont:'',
				formData:{},
				listArr:[],
				addressInfo:{},
				noticeArr:[],
				page:1,
				per_page:1000,
				showSubMask:false,
				activeSub:{},
				
			};
		},
		methods:{
			showImgFn(item){
				this.bannerList = item.goods_img.split(',')
				console.log(this.bannerList)
				this.showImg = true
			},
			showEditFn(){
				this.showEdit = true
				this.editType = 1
			},
			pointsEditAdd(){
				this.showEdit = true
				this.editType = 2
				this.showSubMask = false
			},
			checkPhoneFn(number){
				return !ruleCheck.checkPhone(number)
			},
			downFn(){
				let _this = this
				uni.downloadFile({
					url:this.formData.poster,
					success: (res) => {
						if(res.statusCode==200){
							let filePath = res.tempFilePath;
							wx.getSetting({
							  success(res) {
							    if (!res.authSetting['scope.writePhotosAlbum']) {
							      wx.authorize({
							        scope: 'scope.writePhotosAlbum',
							        success() {
							          _this.saveImageToAlbum(filePath);
							        },
							        fail() {
							          wx.showModal({
							            title: '提示',
							            content: '您拒绝了保存图片到相册的权限，是否去设置页面打开权限？',
							            success(res) {
							              if (res.confirm) {
							                wx.openSetting({
							                  success(settingRes) {
							                    if (settingRes.authSetting['scope.writePhotosAlbum']) {
							                      _this.saveImageToAlbum(filePath);
							                    }
							                  }
							                });
							              }
							            }
							          });
							        }
							      });
							    } else {
							      _this.saveImageToAlbum(filePath);
							    }
							  }
							});
						}else{
							uni.showToast({
								title: '保存失败!',
								icon: 'error'
							});
						}
					}
				})
			},
			saveImageToAlbum(tempFilePath) {
				
			  uni.saveImageToPhotosAlbum({
				filePath: tempFilePath, // 下载后的临时文件路径
				success: () => {
				  uni.showToast({
					title: '保存成功',
					icon: 'success'
				  });
				},
				fail: (err) => {
				  uni.showToast({
					title: '保存失败',
					icon: 'none'
				  });
				}
			  });
			},
			surePointsFn(){
				const data = {
					integral_goods_id:this.activeSub.integral_goods_id,
					user_name:this.addressInfo.consignee_name,
					user_phone:this.addressInfo.consignee_mobile,
					user_address:this.addressInfo.consignee_address,
				}
				this.showSub = true
				api.getRequst(data,'/api/Integral/exchangeGoods').then( (res)=>{
					if(res.code==1){
						uni.showToast({
							title: '兑换成功',
							icon: 'success'
						});
						this.getData()
						this.getPointsFn()
						this.showSub = false
						this.showSubMask = false
					}else{
						
						uni.showToast({
							title: res.msg,
							icon: 'error'
						});
						this.showSub =  false
					}
				}).catch(()=>{
					this.showSub =false
				})
			},
			saveFn(){
				let tipData = {
					bgColor: '#e47470',
					message: '',
					safeAreaInsetTop:true,
				}
				if(this.addressInfo.consignee_name.length<2){
					tipData.message = '收货人不能低于2个字'
					this.$refs.uNotify.show(tipData)
					return false
				}else if(ruleCheck.checkPhone(this.addressInfo.consignee_mobile)){
					tipData.message = ruleCheck.checkPhone(this.addressInfo.consignee_mobile)
					this.$refs.uNotify.show(tipData)
					return false
				}else if(this.addressInfo.consignee_address.length==''){
					tipData.message = '请输入详细的收货地址'
					this.$refs.uNotify.show(tipData)
					return false
				}
				this.showSub = true
				api.getRequst(this.addressInfo,'/api/Integral/editConsigneeInfo').then(async (res)=>{
					if(res.code==1){
						await this.getData()
						this.showSub = false
						if(this.editType==2){
							this.showSubMask = true
						}
						this.showEdit = false
					}else{
						this.showSub =  false
					}
				}).catch(()=>{
					this.showSub =false
				})
			},
			shareRecordFn(){
				uni.navigateTo({
					url: 'shareRecord/shareRecord'
				});
			},
			changeRecordFn(){
				uni.navigateTo({
					url: 'changePoinsRecord/changePoinsRecord'
				});
			},
			getData(){
				api.getRequst({},'/api/Integral').then((res)=>{
					if(res.code==1){
						this.formData = res.data
						this.showShareImg = true
					}
					this.showCont = true
					this.getAddressInfo()
					this.getPointsFn()
				})
			},
			getAddressInfo(){
				api.getRequst({},'/api/Integral/showConsigneeInfo').then((res)=>{
					if(res.code==1){
						this.addressInfo = res.data
					}
				})
			},
			pointsSub(item){
				if(item.goods_status!=1){
					return false
				}
				this.activeSub = item
				if(item.is_yj==2&&this.formData.is_consignee==1){
					this.showEdit = true
					this.editType = 2
				}else{
					this.showSubMask = true
				}
			},
			getPointsFn(){
				let data = {
					page:this.page,
					per_page:this.per_page,
				}
				api.getRequst(data,'/api/Integral/goods').then((res)=>{
					if(res.code==1){
						this.listArr = res.data.data
						this.setNotice()
					}
				})
			},
			getRandomNumber(min, max) {
			  return Math.floor(Math.random() * (max - min + 1)) + min;
			},
			setNotice(){
				for(let i = 0;i<10;i++){
					let goodsName = this.listArr[this.getRandomNumber(0, 10)].goods_name
					let time = this.getRandomNumber(1, 60)
					let user = this.getRandomNumber(100000,999999)
					let obj = "“JLH"+user+"”"+time+"分钟前兑换了“"+goodsName+"”"
					this.noticeArr.push(obj)
				}
			}
		},
		 onLoad() {
			 this.getData()
			 
		}
	}
</script>

<style lang="less">
.showCont{
	height: 100vh;
	background-image: linear-gradient(to bottom right,#3f55cc,#a4c0fe);
	display: flex;
	flex-direction: column;
	.headCont{
		padding: 100rpx 4% 15rpx;
		box-sizing: border-box;
		.erweima{
			width: 100rpx;
			text-align: center;
			font-size: 20rpx;
			color: #fff;
			.iconfont{
				font-size: 70rpx;
			}
		}
		.userCont{
			.userHeadImg{
				width: 160rpx;
				height: 160rpx;
				border-radius: 50%;
				box-sizing: border-box;
				background-color: #fff;
				padding: 5rpx;
				margin: 0 auto;
				image{
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}
			.userHeadName{
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				color: #fff;
				margin-top: 20rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				padding-left: 60rpx;
				box-sizing: border-box;
				.iconfont{
					margin-left: 30rpx;
				}
			}
		}
		.shareCont{
			width: 100%;
			margin: 40rpx auto;
			background-color: rgba(11, 76, 49, 0.2);
			padding:40rpx;
			border-radius: 10rpx;
			color: #fff;
			box-sizing: border-box;
			.cardCont{
				display: flex;
				justify-content: space-between;
				.cardItem{
					text-align: center;
					width: 34%;
					.cardNum{
						font-size: 44rpx;
					}
					.cardName{
						font-size: 24rpx;
						margin-top: 10rpx;
					}
				}
			}
		}
	}
	.pointsCont{
		background-color: rgba(255,255,255,0.8);
		border-radius: 30rpx 30rpx 0 0;
		padding: 30rpx 4%;
		flex: 1;
		height: 100rpx;
		box-sizing:  border-box;
		.pointsHead{
			display: flex;
			align-items: center;
			justify-content: space-between;
			.pointsNotice{
				flex: 1;
			}
			.pointsHeadName{
				display: flex;
				place-items: center;
				font-size: 24rpx;
				color: #333;
				.iconfont{
					margin-left: 5rpx;
				}
			}
		}
		.pointsList{
			display: flex;
			width: 100%;
			flex-wrap: wrap;
			justify-content: space-between;
			padding-top: 30rpx;
			.pointsItem{
				width: 48%;
				background-color: #fff;
				padding: 30rpx;
				border-radius: 20rpx;
				box-sizing: border-box;
				margin-bottom: 30rpx;
				.itemImg{
					width: 100%;
					text-align: center;
					position: relative;
					.is_dq{
						width: 100%;
						position: absolute;
						top: 0;
						left: 0;
						display: flex;
						align-items: center;
						justify-content: center;
						height: 100%;
						color: #f56c6c;
						font-size: 30rpx;
						background-color: rgba(255, 255, 255, 0.6);
					}
					image{
						width: 100%;
					}
				}
				.itemName{
					font-size: 28rpx;
					font-weight: bold;
					margin-top: 10rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
				.itemDes{
					color: #999;
					font-size: 24rpx;
					margin: 10rpx 0 20rpx;
				}
				.itemCtrl{
					display: flex;
					justify-content: space-between;
					.itemCtrlNum{
						display: flex;
						align-items: center;
						font-size: 32rpx;
						font-weight: bold;
						color: #f56c6c;
						.iconfont{
							margin-right: 10rpx;
							font-weight: normal;
						}
					}
					.subPoints{
						padding: 15rpx 40rpx;
						// background-color: #3f55cc;
						background-image: linear-gradient(to right bottom,#a3bffe,#3f55cc);
						color: #fff;
						border-radius: 50rpx;
						font-size: 20rpx;
					}
					.disBtn{
						opacity: 0.5;
					}
				}
			}
		}
	}
}
.tipMask{
	padding: 30rpx;
	image{
		width: 100%;
	}
	.tipName{
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
	}
	.tipNum{
		color: #3f55cc;
		font-size: 30rpx;
		text-align: center;
		margin-top: 30rpx;
	}
	.tipDes{
		font-size: 24rpx;
		color: #666;
		margin-top: 20rpx;
		max-height: 800rpx;
		overflow-y: auto;
	}
	.saveBtn{
		width: 100%;
		padding: 30rpx 0;
		box-sizing: border-box;
		margin-top: 40rpx;
		text-align: center;
		background-image: linear-gradient(to right bottom,#a3bffe,#3f55cc);
		color: #fff;
		border-radius: 50rpx;
		font-size: 22rpx;
	}
}
.formCont{
	padding: 30rpx 4%;
	width: 100%;
	box-sizing: border-box;
	.formHead{
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
	}
	.formMoney{
		text-align: center;
		margin: 30rpx 0;
		color: #3f55cc;
		.moneyText{
			font-size: 40rpx;
			margin-right: 5rpx;
		}
	}
	.formItem{
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		background-color: #f8f8f8;
		padding: 25rpx 50rpx;
		box-sizing: border-box;
		border-radius: 50rpx;
		.inputName {
		  width: 140rpx;
		  position: relative;
		  font-size: 28rpx;
		  .mustIcon {
			font-size: 28rpx;
			top: 0;
			left: -16rpx;
			position: absolute;
			color: #ff5757;
		  }
		}
		.inputOut {
			flex: 1;
			width: 3rem;
			position: relative;
			.inputMask {
				width: 100%;
				height: 100%;
				position: absolute;
				left: 0;
				top: 0;
				z-index: 2;
			  }
			  input{
				width: 100%;
				border: none;
				background: none;
				outline: none;
				position: relative;
				z-index: 1;
			  }
		}
		.inputValue{
			flex: 1;
			width: 100rpx;
		}
		.icon-gouxuan1,.icon-xiala{
			color: #3f55cc;
		}
	}
	.sureBtn{
		width: 100%;
		padding: 25rpx 0;
		text-align: center;
		background-image: linear-gradient(to right bottom,#a3bffe,#3f55cc);
		color: #fff;
		font-size: 24rpx;
		border-radius: 50rpx;
		margin-top: 40rpx;
	}
}
.pointsMask{
	padding: 30rpx 4%;
	box-sizing: border-box;
	.poinsImg{
		display: flex;
		justify-content: center;
		width: 100%;
		image{
			width: 200rpx;
			border-radius: 10rpx;
		}
	}
	.poinsName{
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
		margin: 20rpx 0;
	}
	.poinsNum{
		display: flex;
		justify-content: center;
		align-items: center;
		color: #ff5757;
		font-size: 24rpx;
		.iconfont{
			margin-right: 6rpx;
		}
	}
	.maskTitle{
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.weitText{
			font-size: 30rpx;
			font-weight: bold;
		}
	}
	.maskAddress{
		display: flex;
		margin-top: 20rpx;
		align-items: center;
		justify-content: space-between;
	}
	
}
.war{
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
	.bannerCont{
		width: 700rpx;
		height: 720rpx;
		background-color: #fff;
		padding: 10rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
	}
}
</style>
